 package server.maps;

 import java.awt.Point;
 import java.util.Collections;
 import java.util.LinkedList;
 import java.util.List;

 public class MapleFootholdTree
 {
   private static final byte maxDepth = 8;
/*  11 */   private final List<MapleFoothold> footholds = new LinkedList<>();
   private final Point p1;
   private final Point p2;
   private final Point center;
/*  15 */   private MapleFootholdTree nw = null;
/*  16 */   private MapleFootholdTree ne = null;
/*  17 */   private MapleFootholdTree sw = null;
/*  18 */   private MapleFootholdTree se = null;
/*  19 */   private int depth = 0;
   private int maxDropX;
   private int minDropX;

   public MapleFootholdTree(Point p1, Point p2) {
/*  24 */     this.p1 = p1;
/*  25 */     this.p2 = p2;
/*  26 */     this.center = new Point((p2.x - p1.x) / 2, (p2.y - p1.y) / 2);
   }

   public MapleFootholdTree(Point p1, Point p2, int depth) {
/*  30 */     this.p1 = p1;
/*  31 */     this.p2 = p2;
/*  32 */     this.depth = depth;
/*  33 */     this.center = new Point((p2.x - p1.x) / 2, (p2.y - p1.y) / 2);
   }

   public void insert(MapleFoothold f) {
/*  37 */     if (this.depth == 0) {
/*  38 */       if (f.getX1() > this.maxDropX) {
/*  39 */         this.maxDropX = f.getX1();
       }
/*  41 */       if (f.getX1() < this.minDropX) {
/*  42 */         this.minDropX = f.getX1();
       }
/*  44 */       if (f.getX2() > this.maxDropX) {
/*  45 */         this.maxDropX = f.getX2();
       }
/*  47 */       if (f.getX2() < this.minDropX) {
/*  48 */         this.minDropX = f.getX2();
       }
     }
/*  51 */     if (this.depth == 8 || (f.getX1() >= this.p1.x && f.getX2() <= this.p2.x && f.getY1() >= this.p1.y && f.getY2() <= this.p2.y)) {
/*  52 */       this.footholds.add(f);
     } else {
/*  54 */       if (this.nw == null) {
/*  55 */         this.nw = new MapleFootholdTree(this.p1, this.center, this.depth + 1);
/*  56 */         this.ne = new MapleFootholdTree(new Point(this.center.x, this.p1.y), new Point(this.p2.x, this.center.y), this.depth + 1);
/*  57 */         this.sw = new MapleFootholdTree(new Point(this.p1.x, this.center.y), new Point(this.center.x, this.p2.y), this.depth + 1);
/*  58 */         this.se = new MapleFootholdTree(this.center, this.p2, this.depth + 1);
       }
/*  60 */       if (f.getX2() <= this.center.x && f.getY2() <= this.center.y) {
/*  61 */         this.nw.insert(f);
/*  62 */       } else if (f.getX1() > this.center.x && f.getY2() <= this.center.y) {
/*  63 */         this.ne.insert(f);
/*  64 */       } else if (f.getX2() <= this.center.x && f.getY1() > this.center.y) {
/*  65 */         this.sw.insert(f);
       } else {
/*  67 */         this.se.insert(f);
       }
     }
   }

   public List<MapleFoothold> getAllRelevants() {
/*  73 */     return getAllRelevants(new LinkedList<>());
   }

   private List<MapleFoothold> getAllRelevants(List<MapleFoothold> list) {
/*  77 */     list.addAll(this.footholds);
/*  78 */     if (this.nw != null) {
/*  79 */       this.nw.getAllRelevants(list);
/*  80 */       this.ne.getAllRelevants(list);
/*  81 */       this.sw.getAllRelevants(list);
/*  82 */       this.se.getAllRelevants(list);
     }
/*  84 */     return list;
   }

   private List<MapleFoothold> getRelevants(Point p) {
/*  88 */     return getRelevants(p, new LinkedList<>());
   }

   private List<MapleFoothold> getRelevants(Point p, List<MapleFoothold> list) {
/*  92 */     list.addAll(this.footholds);
/*  93 */     if (this.nw != null) {
/*  94 */       if (p.x <= this.center.x && p.y <= this.center.y) {
/*  95 */         this.nw.getRelevants(p, list);
/*  96 */       } else if (p.x > this.center.x && p.y <= this.center.y) {
/*  97 */         this.ne.getRelevants(p, list);
/*  98 */       } else if (p.x <= this.center.x && p.y > this.center.y) {
/*  99 */         this.sw.getRelevants(p, list);
       } else {
/* 101 */         this.se.getRelevants(p, list);
       }
     }
/* 104 */     return list;
   }


   private MapleFoothold findWallR(Point p1, Point p2) {
/* 109 */     for (MapleFoothold f : this.footholds) {
/* 110 */       if (f.isWall() && f.getX1() >= p1.x && f.getX1() <= p2.x && f.getY1() >= p1.y && f.getY2() <= p1.y) {
/* 111 */         return f;
       }
     }
/* 114 */     if (this.nw != null) {
/* 115 */       if (p1.x <= this.center.x && p1.y <= this.center.y) {
/* 116 */         MapleFoothold ret = this.nw.findWallR(p1, p2);
/* 117 */         if (ret != null) {
/* 118 */           return ret;
         }
       }
/* 121 */       if ((p1.x > this.center.x || p2.x > this.center.x) && p1.y <= this.center.y) {
/* 122 */         MapleFoothold ret = this.ne.findWallR(p1, p2);
/* 123 */         if (ret != null) {
/* 124 */           return ret;
         }
       }
/* 127 */       if (p1.x <= this.center.x && p1.y > this.center.y) {
/* 128 */         MapleFoothold ret = this.sw.findWallR(p1, p2);
/* 129 */         if (ret != null) {
/* 130 */           return ret;
         }
       }
/* 133 */       if ((p1.x > this.center.x || p2.x > this.center.x) && p1.y > this.center.y) {
/* 134 */         MapleFoothold ret = this.se.findWallR(p1, p2);
/* 135 */         if (ret != null) {
/* 136 */           return ret;
         }
       }
     }
/* 140 */     return null;
   }

   public MapleFoothold findWall(Point p1, Point p2) {
/* 144 */     if (p1.y != p2.y) {
/* 145 */       throw new IllegalArgumentException();
     }
/* 147 */     return findWallR(p1, p2);
   }


   public boolean checkRelevantFH(short fromx, short fromy, short tox, short toy) {
/* 152 */     MapleFoothold fhdata = null;
/* 153 */     for (MapleFoothold fh : this.footholds) {
/* 154 */       if (fh.getX1() <= fromx && fh.getX2() >= fromx && fh.getY1() <= fromy && fh.getY2() >= fromy) {
/* 155 */         fhdata = fh;
         break;
       }
     }
/* 159 */     for (MapleFoothold fh2 : this.footholds) {
/* 160 */       if (fh2.getX1() <= tox && fh2.getX2() >= tox && fh2.getY1() <= toy && fh2.getY2() >= toy) {
/* 161 */         if (fhdata.getId() != fh2.getId() && fh2.getId() != fhdata.getNext() && fh2.getId() != fhdata.getPrev()) {
/* 162 */           System.out.println("Couldn't find the correct pos for next/prev");
/* 163 */           return false;
         }
/* 165 */         return true;
       }
     }
/* 168 */     return false;
   }

   public MapleFoothold findBelow(Point p, boolean byCharacter) {
/* 172 */     List<MapleFoothold> relevants = getRelevants(p);
/* 173 */     List<MapleFoothold> xMatches = new LinkedList<>();
/* 174 */     for (MapleFoothold fh : relevants) {
/* 175 */       if (fh.getX1() <= p.x && fh.getX2() >= p.x) {
/* 176 */         xMatches.add(fh);
       }
     }
/* 179 */     Collections.sort(xMatches);
/* 180 */     for (MapleFoothold fh : xMatches) {
/* 181 */       if (fh.isWall()) {
         continue;
       }
/* 184 */       if (fh.getY1() != fh.getY2()) {
         int calcY;
/* 186 */         double s1 = Math.abs(fh.getY2() - fh.getY1());
/* 187 */         double s2 = Math.abs(fh.getX2() - fh.getX1());
/* 188 */         double s4 = Math.abs(p.x - fh.getX1());
/* 189 */         double alpha = Math.atan(s2 / s1);
/* 190 */         double beta = Math.atan(s1 / s2);
/* 191 */         double s5 = Math.cos(alpha) * s4 / Math.cos(beta);
/* 192 */         if (fh.getY2() < fh.getY1()) {
/* 193 */           calcY = fh.getY1() - (int)s5;
         } else {
/* 195 */           calcY = fh.getY1() + (int)s5;
         }
/* 197 */         if (calcY >= p.y)
/* 198 */           return fh;  continue;
       }
/* 200 */       if (byCharacter ? (fh.getY1() == p.y) : (fh.getY1() >= p.y)) {
/* 201 */         return fh;
       }
     }
/* 204 */     return null;
   }

   public int getX1() {
/* 208 */     return this.p1.x;
   }

   public int getX2() {
/* 212 */     return this.p2.x;
   }

   public int getY1() {
/* 216 */     return this.p1.y;
   }

   public int getY2() {
/* 220 */     return this.p2.y;
   }

   public int getMaxDropX() {
/* 224 */     return this.maxDropX;
   }

   public int getMinDropX() {
/* 228 */     return this.minDropX;
   }
 }


/* Location:              C:\PGX\Server.jar!\server\maps\MapleFootholdTree.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */